package com.atguigu.gmall.item.controller;

/*商品详情汇总数据*/

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@RestController
@RequestMapping("/api/item")
public class ItemApiController {

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ListFeignClient listFeignCliet;

    //数据汇总
    @GetMapping("/getItem/{skuId}")
    public Map<String, Object> getItem(@PathVariable Long skuId) {
        Map<String, Object> result = new HashMap<>();

        //查询5部分数据，不需要事务，因此不需要建service层，当然也可以建
        //思路：因为sku最为重要，因为获取到skuInfo表的信息，其他的信息也能够获取到，因此先获取库存信息

        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //1、库存信息
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            result.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);

        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {
            //2、获取三级分类
            BaseCategoryView baseCategoryView = productFeignClient.getBaseCategoryView(skuInfo.getCategory3Id());
            result.put("categoryView", baseCategoryView);
        }, threadPoolExecutor);


        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
            //3、获取价格
            BigDecimal price = productFeignClient.getPrice(skuId);
            result.put("price", price);

        }, threadPoolExecutor);


        CompletableFuture<Void> spuSaleAttrListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {
            //4、获取spuAttr
            List<SpuSaleAttr> saleAttrAndValueAndChecked = productFeignClient.getSaleAttrAndValueAndChecked(skuInfo.getSpuId(), skuId);
            result.put("spuSaleAttrList", saleAttrAndValueAndChecked);
        }, threadPoolExecutor);

        CompletableFuture<Void> valuesSkuJsonCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {
            //5、获取组合
            Map<Object, Object> skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            result.put("valuesSkuJson", JSONObject.toJSONString(skuValueIdsMap));
        }, threadPoolExecutor);

        //6、增加热度(此功能不重要，不用让其他线程等待，没加上就没加上，不用管，所以和其他线程没关系，也不用放到下面的等待括号中)
        CompletableFuture.runAsync(()->{
            listFeignCliet.hotScore(skuId);
        },threadPoolExecutor);



        //等待所有线程结束
        CompletableFuture.allOf(skuInfoCompletableFuture,categoryViewCompletableFuture,priceCompletableFuture,spuSaleAttrListCompletableFuture,
                valuesSkuJsonCompletableFuture).join();//注意不要忘记jion

        return result;

    }
}
